import {
  Module,
  Global,
  MiddlewareConsumer,
  NestModule,
  forwardRef,
  RequestMethod,
} from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { PersonModule } from './person/person.module';
import { PersonService } from './person/person.service';
import { XxService } from './person/xx.service';

import { LogMiddleware } from './log.middleware'; // 中间件
import { APP_GUARD, APP_INTERCEPTOR, APP_FILTER } from '@nestjs/core';
import { LoginGuard } from './login.guard';
import { TimeInterceptor } from './time.interceptor';

import { DddService } from './ddd.service';
import { CccService } from './ccc.service';

import { TestFilter } from './test.filter';

@Global() // 设置为全局的，其他模块依赖时，可以直接使用
@Module({
  imports: [PersonModule.register({ name: 'person', age: 200000 })], // 动态引入模块
  controllers: [AppController],
  providers: [
    // {
    //   provide: 'personNew',
    //   useExisting: AppService // 为AppService创建别名
    // },
    // {
    //   provide: APP_GUARD,
    //   useClass: LoginGuard,
    // }, // // 设置全局守卫 方式2
    AppService,
    XxService,
    CccService,
    DddService,
    // {
    //   provide: APP_FILTER,
    //   useClass: TestFilter,
    // },
    {
      provide: APP_INTERCEPTOR,
      useClass: TimeInterceptor,
    },
    {
      provide: 'person',
      useValue: {
        name: '张三哥哥',
        age: 18,
      },
    },
    {
      provide: 'person2',
      useFactory(person: { name: string }, appService: AppService) {
        return {
          name: `这是person2的依赖：${person.name}`,
          age: appService.getHello(),
        };
      },
      inject: ['person', AppService],
    },
  ],
  exports: [AppService, 'person2'],
})
export class AppModule {
  // 路由中间件 配置需要启用的路由
  // configure(consumer: MiddlewareConsumer) {
  //   consumer.apply(LogMiddleware).forRoutes({
  //     path: '*',
  //     method: RequestMethod.ALL,
  //   });
  //   consumer
  //     .apply(LogMiddleware)
  //     .forRoutes({ path: 'person', method: RequestMethod.GET });
  // }
}
